package com.cssw.fyzb.busi.api.user;

import com.cssw.fyzb.busi.convert.Converter;
import com.cssw.fyzb.core.model.vo.user.PublicUserInfoVo;
import com.cssw.fyzb.core.model.vo.video.VideoVo;
import com.cssw.fyzb.busi.service.user.UserRelService;
import com.cssw.fyzb.busi.service.user.UserService;
import com.cssw.fyzb.core.model.dodb.User;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.*;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 粉丝列表（粉丝用户数据）
 *
 * @author Feng Chen
 */
@Function(description = "关注列表（粉丝用户数据）", parameters = {
        @Parameter(name = "userId", type = Type.Integer, description = "用户标识", required = true),
        @Parameter(name = "pageNumber", type = Type.Integer, description = "当前页，从1开始", required = true),
        @Parameter(name = "pageSize", type = Type.Integer, description = "页大小", required = true),
}, result = @Result(
        properties = {
                @Property(name = "total", type = Type.Integer, description = "总页数"),
                @Property(name = "count", type = Type.Integer, description = "总行数"),

        },
        listProperties = {
                @ListProperty(name = "list", properties = {
                        @Property(name = "userId", type = Type.Integer, description = "用户编号"),
                        @Property(name = "supUserId", type = Type.Integer, description = "推荐用户编号"),
                        @Property(name = "userName", type = Type.String, description = "用户姓名"),
                        @Property(name = "nickName", type = Type.String, description = "用户昵称"),
                        @Property(name = "mobile", type = Type.String, description = "手机号码"),
                        @Property(name = "sex", type = Type.String, description = "性别"),
                        @Property(name = "faceAuth", type = Type.String, description = "是否人脸认证"),
                        @Property(name = "sign", type = Type.String, description = "用户个性签名"),
                        @Property(name = "avatar", type = Type.String, description = "用户头像信息"),
                })
        }
))

@Component
public class ListFriend implements Action {

    @Autowired
    private UserService userService;

    @Autowired
    private UserRelService userRelService;

    private static final Logger logger = LoggerFactory.getLogger(ListFans.class);

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        Integer pageNumber = request.getInteger("pageNumber");
        Integer pageSize = request.getInteger("pageSize");
        try {
            Page<User> page = userService.pageUserFriend(userId, pageNumber, pageSize);

            List<PublicUserInfoVo> list = ((Converter<List<User>, List<PublicUserInfoVo>>) users -> {
                List<PublicUserInfoVo> vos = new ArrayList<>();
                users.forEach((v) -> {
                    PublicUserInfoVo vo = new PublicUserInfoVo();
                    vo.setAvatar(v.getAvatar());
                    vo.setNickName(v.getNickName());
                    vo.setUserId(v.getUserId());
                    vo.setMobile(v.getMobile());
                    vo.setUserName(v.getUserName());
                    vo.setSex(v.getSex());
                    vo.setSign(v.getSign());
                    vo.setBirthday(v.getBirthday());
                    vo.setFaceAuth(v.getFaceAuth());
                    Long relType = userRelService.getRelType(userId, v.getUserId());
                    vo.setRelType(relType);
                    vos.add(vo);
                });
                return vos;
            }).convert(page.getList());
            return Response.success().put("list", list).put("total", page.getTotalPage()).put("count", page.getTotalRow());
        } catch (Exception e) {
            logger.info("", e);
            return Response.fail("操作异常");
        }
    }

}
